﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace StarForce {
    public class UiCardGraveyardSort : MonoBehaviour {
        [SerializeField] private Transform Transform;
        [SerializeField] private UiCardParameters Parameters;
        [SerializeField] private UiCardGraveyard CardGraveyard;

        private void Awake() {
            CardGraveyard.OnPileChange += Sort;
        }

        private void Sort(Card[] cards) {
            if (cards == null) {
                throw new ArgumentNullException("Can't sort a card list null");
            }

            int lastIndex = cards.Length - 1;
            Card lastCard = cards[lastIndex];
            Vector3 backPos = Transform.position;
            Vector3 gravePos = backPos + new Vector3(0, 0, -5);
                
            lastCard.MoveToWithZ(gravePos,Parameters.MovementSpeed);
            
            for (var i = 0; i < cards.Length - 1; i++)
            {
                var card = cards[i];
                card.MoveToWithZ(backPos, Parameters.MovementSpeed);
            }
        }
    }
}

